   package com.javalong.affairs.module.notifything.activity;

import java.util.HashMap;
import java.util.Map;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.javalong.affairs.R;
import com.javalong.affairs.base.activity.UIHandlerActivity;
import com.javalong.affairs.base.common.ServiceLocator;
import com.javalong.affairs.base.common.UIHandler;
import com.javalong.affairs.base.constant.BundleKey;
import com.javalong.affairs.base.constant.DataType;
import com.javalong.affairs.base.constant.RequestKey;
import com.javalong.affairs.base.listener.BaseResponseListener;
import com.javalong.affairs.base.manager.IntentManager;
import com.javalong.affairs.base.model.MessageInfo;
import com.javalong.affairs.base.ui.TopTitle;
import com.javalong.affairs.base.utils.FileUtils;
import com.javalong.affairs.base.utils.HttpUtils;
import com.javalong.affairs.base.utils.StringUtils;
import com.javalong.affairs.base.utils.TimeUtils;
import com.javalong.affairs.base.utils.ToastUtils;
import com.javalong.affairs.base.utils.UrlUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;

@ContentView(R.layout.activity_watchsendmsg)
public class WatchSendMsgActivity extends UIHandlerActivity {
	@ViewInject(R.id.tv_time)
	private TextView tv_time;
	@ViewInject(R.id.tv_address)
	private TextView tv_address;
	@ViewInject(R.id.tv_sender)
	private TextView tv_sender;
	@ViewInject(R.id.tv_receiver)
	private TextView tv_receiver;
	@ViewInject(R.id.tv_status)
	private TextView tv_status;
	@ViewInject(R.id.tv_content)
	private TextView tv_content;
	@ViewInject(R.id.bt_file)
	private Button bt_file;
	@ViewInject(R.id.tv_timelimit)
	private TextView tv_timelimit;
	@ViewInject(R.id.ui_toptitle)
	private TopTitle topTitle;
	@ViewInject(R.id.tv_title_content)
	private TextView tv_title;
	@ViewInject(R.id.bt_update)
	private Button bt_update;
	@ViewInject(R.id.bt_delete)
	private Button bt_delete;
	@ViewInject(R.id.bt_search)
	private Button bt_search;
	@ViewInject(R.id.bt_search_audit)
	private Button bt_search_audit;
	private String msgId;
	private String status;
	private boolean isReceiver;
	private String simpleMsgId;
	private MessageInfo msgInfo;
	private String statusStr;
	@ViewInject(R.id.tv_type)
	private TextView tv_type;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		Bundle bundle = getIntent().getBundleExtra(BundleKey.BUNDLE);
		msgId = bundle.getString(BundleKey.MSGID);
		simpleMsgId = bundle.getString(BundleKey.SIMPLEMSGID);
		status = bundle.getString(BundleKey.SIMPLEMSGSTATUS);
		statusStr = bundle.getString(BundleKey.STATUS);
		isReceiver = bundle.getBoolean(BundleKey.ISRECEIVER);
		boolean nodelete = bundle.getBoolean(BundleKey.NODELETE);
		if (nodelete) {
			bt_delete.setVisibility(View.GONE);
		}
		initData();
	}

	private void initData() {
		String url = UrlUtils.getUrl("SendMessage", "outOne_messageinfo");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(RequestKey.ID, simpleMsgId);
		HttpUtils.post(url, params, new BaseResponseListener(DataType.MESSAGE) {
			@Override
			public void onSuccess(Object obj) {
				super.onSuccess(obj);
				initUI((MessageInfo) obj);
			}
		});
	}

	private void initUI(MessageInfo msgInfo) {
		this.msgInfo = msgInfo;
		if (msgInfo != null) {
			tv_time.setText(TimeUtils.getFormatTime2(msgInfo.time));
			tv_address.setText(msgInfo.place);
			tv_sender.setText(msgInfo.nameSend);
			tv_receiver.setText(msgInfo.recipient);
			tv_content.setText(msgInfo.messageData);
			tv_timelimit.setText(msgInfo.timeLimit);
			tv_title.setText(msgInfo.title);
			tv_status.setText(statusStr);
			tv_type.setText(StringUtils.getMessageType(msgInfo.messageType));
		}
		topTitle.setOnLeftDrawableClick(this);
		bt_update.setOnClickListener(this);
		bt_search.setOnClickListener(this);
		bt_search_audit.setOnClickListener(this);
		bt_delete.setOnClickListener(this);
		bt_file.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		super.onClick(v);
		Bundle bundle = new Bundle();
		switch (v.getId()) {
		case R.id.bt_update:
			bundle.putString(BundleKey.SIMPLEMSGID, simpleMsgId);
			IntentManager.doActivity(this, UpdateMsgActivity.class, bundle);
			break;
		case R.id.bt_search:
			bundle.putString(BundleKey.SIMPLEMSGID, simpleMsgId);
			IntentManager.doActivity(this, MsgStatisticActivity.class, bundle);
			break;
		case R.id.bt_search_audit:
			bundle.putString(BundleKey.SIMPLEMSGID, simpleMsgId);
			bundle.putString(BundleKey.TIME, msgInfo.time);
			bundle.putString(BundleKey.TITLE, msgInfo.title);
			IntentManager
					.doActivity(this, AuditStatisticActivity.class, bundle);
			break;
		case R.id.bt_delete:
			delete();
			break;
		case R.id.bt_file:
			if (msgInfo != null && msgInfo.attachment != null
					&& msgInfo.attachment.size() > 0
					&& !StringUtils.isEmpty(msgInfo.attachment.get(0).download)) {
				String downloadFilePath = StringUtils.getRandomFilePath();
				FileUtils.download(bt_file, UrlUtils.getBaseUrl()
						+ msgInfo.attachment.get(0).download, downloadFilePath);
			}
			break;
		default:
			break;
		}
	}

	private void delete() {
		String url = UrlUtils.getUrl("SendMessage", "delete");
		Map<String, Object> params = new HashMap<String, Object>();
		params.put(RequestKey.ID, simpleMsgId);
		HttpUtils.post(url, params, new BaseResponseListener(null) {
			@Override
			public void onSuccess(Object obj) {
				super.onSuccess(obj);
				ToastUtils.showMessage("删除成功");
				UIHandler uiHandler = ServiceLocator.GetInstance().getInstance(
						UIHandler.class);
				uiHandler.postCallback(
						R.string.event_refresh_notification_send, null);
				finish();
			}
		});
	}
}
